﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Kingdee.WeksErp.DAL;
using Kingdee.WeksErp.Models.Orders;
using Quartz;
using log4net;

namespace Kingdee.WeksErp.Jobs
{
    public class FxJob : IJob, IDisposable
    {
        private WeksErpContext db = new WeksErpContext();
        ILog log = LogManager.GetLogger("JobLog");
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                var list = db.Database.SqlQuery<SyncResult>(" select top 30 FID, FSellerMessage  from IC_Web2ERPOrders where  ( FIsJx<>1 OR FIsJx IS NULL)").ToList();
                log.Error("list.count:" + list.Count);
                foreach (var sync in list)
                {
                    try
                    {
                        int count = UpdateExtInfo(sync);
                        log.InfoFormat("FID:{0},Ret:{1},Content:{2}", sync.FID, count, sync.FSellerMessage);
                    }
                    catch (Exception ex)
                    {

                        log.Error(ex.Message, ex);
                    }
                }
            }
            catch (Exception ex)
            {

                log.Error(ex.Message, ex);
            }
        }
        private int UpdateExtInfo(SyncResult syncResult)
        {
            var sb = new StringBuilder();
            var sellerMsg = ConvertToSellerMsg(syncResult);
            sb.AppendLine(
                "update IC_Web2ERPOrders set FKpInfo=@FKpInfo,FYqfh=@FYqfh,FDt=@FDt,FQzkh=@FQzkh,FZp=@FZp,FXhbg=@FXhbg,Ffpyjdz=@Ffpyjdz,FIsJx=1,FJxTime=getdate() where FID=@FOrderId ;");

            SqlParameter[] paras =
            {
                new SqlParameter("@FKpInfo",sellerMsg.FKpInfo),
                new SqlParameter("@FYqfh",sellerMsg.FYqfh),
                new SqlParameter("@FDt",sellerMsg.FDt),
                new SqlParameter("@FQzkh",sellerMsg.FQzkh),
                new SqlParameter("@FZp",sellerMsg.FZp),
                new SqlParameter("@FXhbg",sellerMsg.FXhbg),
                new SqlParameter("@FOrderId",syncResult.FID),
                new SqlParameter("@Ffpyjdz",sellerMsg.Ffpyjdz)
            };
            return db.Database.ExecuteSqlCommand(sb.ToString(), paras);
        }

        private SellerMsg ConvertToSellerMsg(SyncResult syncResult)
        {
            var sellerMsg = new SellerMsg()
            {
                FKpInfo = "",
                FDt = "",
                Ffpyjdz = "",
                FHpfx = "",
                FQzkh = "",
                FXhbg = "",
                FYqfh = "",
                FZp = ""
            };
            // 开票信息
            /*
             发票抬头：
             -----------------
             公司名称：
             公司地址：
             固定号码：
             开户行：
             开户行账号：
             税号：
             */
            var rule = @"发票抬头(：|:)((\w)+)";
            var m = Regex.Match(syncResult.FSellerMessage, rule);
            if (m.Success)
            {
                sellerMsg.FKpInfo = m.Groups[2].Value;
            }


            rule = @"公司名称(：|:)[\s\S]+?]";
            m = Regex.Match(syncResult.FSellerMessage, rule);
            if (m.Success)
            {
                sellerMsg.FKpInfo += m.Value.Trim(']');
            }
            // 延期发货
            rule = @"延期发货(：|:)((\w)+)";
            m = Regex.Match(syncResult.FSellerMessage, rule);
            if (m.Success)
            {
                sellerMsg.FYqfh = m.Groups[2].Value;
            }
            //好评返现
            rule = @"好评返现(：|:)((\w)+)";
            m = Regex.Match(syncResult.FSellerMessage, rule);
            if (m.Success)
            {
                sellerMsg.FHpfx = m.Groups[2].Value;
            }
            //电梯
            rule = @"电梯(：|:)((\w)+)";
            m = Regex.Match(syncResult.FSellerMessage, rule);
            if (m.Success)
            {
                sellerMsg.FDt = m.Groups[2].Value;
            }
            //潜在客户
            rule = @"潜在客户(：|:)((\w)+)";
            m = Regex.Match(syncResult.FSellerMessage, rule);
            if (m.Success)
            {
                sellerMsg.FQzkh = m.Groups[2].Value;
            }
            //赠品
            rule = @"赠品(：|:)((\w)+)";
            m = Regex.Match(syncResult.FSellerMessage, rule);
            if (m.Success)
            {
                sellerMsg.FZp = m.Groups[2].Value;
            }
            //型号变更
            rule = @"型号(：|:)((\w)+)";
            m = Regex.Match(syncResult.FSellerMessage, rule);
            if (m.Success)
            {
                sellerMsg.FXhbg = m.Groups[2].Value;
            }

            rule = @"发票邮寄地址(：|:)((\w)+)";
            m = Regex.Match(syncResult.FSellerMessage, rule);
            if (m.Success)
            {
                sellerMsg.Ffpyjdz = m.Groups[2].Value;
            }

            return sellerMsg;
        }

        void IDisposable.Dispose()
        {
            db.Dispose();
        }
    }
}